## title: Replication script for From Clubs to Hubs: Analysing Lobbying Networks in EU Financial Regulation After Crisis
## author: Bastiaan Redert

# Load packages

library(tidyverse)
library(datasets)
library(pacman)
library(readxl)
library('reldist')
library('Matrix')
library("tibble")
library("ineq")
pacman::p_load(pacman, dplyr, GGally, ggplot2, ggthemes, plotly, 
               lubridate, rio, tidyr, stringr, httr, 
               rmarkdown, igraph, network, tibble, RColorBrewer, sna, statnet)

#Load data

resful <- read_excel("~/Desktop/Data paper 3/basfinal_paper3.xlsm")

## CONSTRUCTING ADJACENCY MATRICES USING CONSULTATION DATA

# The code below creates individual networks filtered per agency (EBA; EIOPA; or ESMA), per time period (pre-crisis [pre]; post-crisis [post]; or post-reforms[post2]. 
# To create the different networks mentioned in the paper, one has to repeat this script per agency, per time period. 

responses <- filter(resful, NameAG == "EBA", Period == 'pre')

## Creating edgelist for two-mode actor-consultation network
as_tibble(responses)
actors <- responses %>%
  distinct(OrgID) %>%
  rename(label = OrgID)
cons <- responses %>%
  distinct(ConsultationID) %>%
  rename(label = ConsultationID)
nodes <- full_join(actors, cons, by = "label")
nodes <- nodes %>% rowid_to_column("id")
per_route <- responses %>%  
  group_by(OrgID, ConsultationID) %>%
  summarise(weight = n()) %>% 
  ungroup()
edges <- per_route %>% 
  left_join(nodes, by = c("OrgID" = "label")) %>% 
  rename(from = id)
edges <- edges %>% 
  left_join(nodes, by = c("ConsultationID" = "label")) %>% 
  rename(to = id)
fedges <- select(edges, from, to)

## Two mode network conversion to one-mode actor-actor network
m <- table(fedges)
M <- as.matrix(m)
A <- spMatrix(nrow=length(unique(fedges$from)),
              ncol=length(unique(fedges$to)),
              i = as.numeric(factor(fedges$from)),
              j = as.numeric(factor(fedges$to)),
              x = rep(1, length(as.numeric(fedges$from))) )
row.names(A) <- levels(factor(fedges$from))
colnames(A) <- levels(factor(fedges$to))

# Adjacency matrices for actors (Arow) or consultations (Acol) ADJACENCY MATRICES FOR ACTORS (ROW) AND CONSULTATIONS (COL)
Arow <- A %*% t(A)
Acol <- t(A) %*% A

## CREATING NETWORKS USING iGRAPH NETWORKS

# Load packages

library(igraph)
library(tidygraph)
library(ggraph)
library(sna)

# Creating iGraph network
net <- graph_from_adjacency_matrix(Arow, weighted = TRUE, diag = FALSE, mode = c("undirected"))

## SET VERTEX ATTRIBUTES (LINK NETWORK WITH OG DATASET)
V(net)$name <- (actors$label)
V(net)$type <- sapply(V(net)$name, function(x) as.factor(responses$Org..Structure[responses$OrgID == x]))
V(net)$lom <- sapply(V(net)$name, function(x) as.character(responses$LoM[responses$OrgID == x]))
V(net)$sec <- sapply(V(net)$name, function(x) as.factor(responses$Econ..Sector[responses$OrgID == x]))
V(net)$mem <- sapply(V(net)$name, function(x) responses$No..Members[responses$OrgID == x])
V(net)$degree <- degree(net, loops = FALSE, normalized = TRUE)
E(net)$weight <- edge_attr(net)$weight

## NETWORK VISUALIZATION

V(net)$color <- "grey60"
E(net)$color <- "grey70"

## VERTEX LABEL DEGREE
# -pi/2: above
# 0: to the right of
# pi/2: below
# pi: to the left of

set.seed(1995)
plot(net, 
     vertex.size = 2,  
     vertex.label.cex = 0.00001,
     vertex.label.family = "Times",
     vertex.label.color = "black",
     vertex.label.dist = 1.5,
     vertex.label.degree = -2/pi,
     edge.arrow.size= 0, 
     edge.width = E(net)$weight*0.1,
     edge.curved=0.3,
     layout=layout_with_lgl,
     main = "Lobbying Network EBA (2004-2007)")

set.seed(1995)
plot(net, 
     vertex.size = 5,  
     vertex.label.cex = 0.7,
     vertex.label.family = "Times",
     vertex.label.color = "black",
     vertex.label.dist = 1.5,
     vertex.label.degree = -2/pi,
     edge.arrow.size= 0,
     edge.curved=0.3,
     layout=layout_with_fr)

## EXPECTATION 1: DENSITY
# Size is determined by the number of actors in the network (V(net)) and the number of edges (E(net)). 
V(net)
E(net)

## EXPECTATION 2: INTERCONNECTEDNESS USING DENSITY
graph.density(net,loops=FALSE)
network.density(net)
?graph.density

## EXPECTATION 3: BALKANIZATION USING WALKTRAP COMMUNITY DETECTION & MODULARITY
wtc <- cluster_walktrap(net)
communities(wtc)
modularity(net, membership(wtc), weights = E(net)$weight)

## EXPECTATION 4: CENTRALIZATION

# Eigenvector centralization (E4)
centr_eigen(net, directed = FALSE, scale = TRUE, normalized = TRUE)$centralization

# Eigenvector centrality
eigen <- eigen_centrality(net, directed = FALSE, weights = E(net)$weight)
eigen <- as.data.frame(eigen)
summary(eigen$vector)

# Distribution eigenvector centrality with Gini-scores. Repeat this for each network, and create Lc for each of the nine networks (Lc1-Lc9)

ineq(eigen$vector, type = "Gini")
Lc1 <- Lc(eigen$vector)

# Plot Lorenz Curves using Lc1-9

plot(Lc1, general = TRUE,col="black")
lines(Lc2, general = TRUE,col="darkgrey")
lines(Lc3, general = TRUE,col="grey")

plot(Lc4, general = TRUE, col="black")
lines(Lc5, general = TRUE, col="darkgrey")
lines(Lc6, general = TRUE, col="grey")

plot(Lc7, general = TRUE, col="black", lwd=2)
lines(Lc8, general = TRUE, col="darkgrey", lwd=2)
lines(Lc9, general = TRUE, col="grey", lwd=2)


## VISUALIZATION OF NETWORKS USING NetBioV (SEE APPENDIX)

#Load packages
if (!requireNamespace("BiocManager", quietly = TRUE))
  install.packages("BiocManager")
BiocManager::install("netbiov")
library("netbiov")
install.packages("viridis") 
library("viridis")

# Visualize networks with NetBioV and Viridis. Change viridis(n) to number of actors in the network.
set.seed(1995)
xx <- plot.modules(net, 
                   mod.list = communities(wtc),
                   color.random = FALSE,
                   modules.name.num = TRUE,
                   col.grad = viridis(252),
                   expression = exp,
                   v.size = 1, sf = 60, e.width = 0.05, bg = "white",
                   layout.function = layout.fruchterman.reingold)

## CONDITIONAL UNIFORM GRAPH - TESTS 

#Load packages
library("intergraph")

# Create sna-networks
net.sna <- asNetwork(net)
cg <- cug.test(net.sna, gden, mode = 'graph', cmode = "size", reps = 5000)
plot(cg, main = "ESMA Post-reforms", ylab = "Size")

